<?php  
if(!isset($_GET['id'])){echo"DATA BELUM TERSEDIA"; exit;}
$karyawan = loaddata_karyawan($_GET['id']);  
$m = isset( $_GET['m'] ) ? $_GET['m']:  date('m');
$y = isset( $_GET['y'] ) ? $_GET['y']:  date('Y');
$date = isset( $_GET['date'] ) ? $_GET['date']:  date('Y-m-d');
if(!$karyawan){echo"DATA BELUM TERSEDIA"; exit;}
 

function list_data_absen($tpar ,$month,$year,$id){
	global $timedeposit,$timedepositgo;
	if($tpar == 1){
		$startdate = $year.'-'.$month.'-01';
		$enddate = $year.'-'.$month.'-15';
	}elseif($tpar == 2){
		$startdate = $year.'-'.$month.'-16';$_use31 = checkdate  (    $month  ,    31  ,    $year  );
		$_use30 = checkdate  (    $month  ,    30  ,    $year  );
		$_use29 = checkdate  (    $month  ,    29  ,    $year  );
			if($_use31) $enddate = $year.'-'.$month.'-31';
		elseif($_use30) $enddate = $year.'-'.$month.'-30';
		elseif($_use29) $enddate = $year.'-'.$month.'-29';
		  else 			$enddate = $year.'-'.$month.'-28';
	
	}
	
	
	
	$header = array(  
		'Tanggal'=>array('style'=>'border-bottom:2px solid;border-top:2px solid;width:16%'),  
		'Jadwal <br>datang'  =>array('style'=>'border-bottom:2px solid;border-top:2px solid;width:11%'),  
		'Jadwal <br>Pulang'  =>array('style'=>'border-bottom:2px solid;border-top:2px solid;width:11%'),  
		'Waktu <br>datang'  =>array('style'=>'border-bottom:2px solid;border-top:2px solid;width:11%'),  
		'Waktu <br>Pulang'  =>array('style'=>'border-bottom:2px solid;border-top:2px solid;width:11%'),  
		'Datang'  =>array('style'=>'border-bottom:2px solid;border-top:2px solid;width:8%'),  
		'Pulang'  =>array('style'=>'border-bottom:2px solid;border-top:2px solid;width:8%'),  
		'Tidak <br>hadir'  =>array('style'=>'border-bottom:2px solid;border-top:2px solid;width:10%'),  
		'Keterangan'=>array('style'=>'border-bottom:2px solid;border-top:2px solid;width:15%'),    
	);
	$timedeposit = 0;
	$dates = list_kalender($startdate , $enddate);
	$rows =array();
	foreach($dates as $date){
		$query = "SELECT * FROM absensi   WHERE  tanggal_kerja = DATE('{$date}') AND karyawan_id = {$id} ";
		$result = my_query($query);
		if(my_num_rows($result) > 0){ 
			$names = my_fetch_array($result);  
		}else{
			$n=
			$names = '-';
		}
		 
		$sfp=$sfd = '-';$tds=0;
		if( date('Y-m-d 00:00:00')  >= $date .' 00:00:00'  ){
			
			if($names <>'-'){ 
				list($yyyy,$mm,$dd) = explode("-" , $names['tanggal_kerja'] );
				
				//HITUNG WAKTU DATANG 
				list($hf,$mf,$sf) = explode(":" , $names['faktanya_datang'] );
				$faktanya_datang 	= mktime( $hf, $mf, $sf, (int)$mm, (int)$dd, $yyyy); 
				list($hs,$ms,$ss) 	= explode(":" , $names['seharusnya_datang'] );
				$seharusnya_datang 	=  mktime( $hs, $ms, $ss, (int)$mm, (int)$dd, $yyyy); 
					
					if($seharusnya_datang >= $faktanya_datang){
						$luang_datang 		= $seharusnya_datang - $faktanya_datang; 
						if ($luang_datang >= 0) {
							$mins = floor ($luang_datang / 60);
							$secs = $luang_datang % 60;
							$sfd =  (int) sprintf ("%d:%2.1f",  $mins, $secs);
							
						}
						$timedeposit += $sfd;
					}else{
						$luang_datang 		= $faktanya_datang - $seharusnya_datang  ; 
						if ($luang_datang > 0) {
							$mins = floor ($luang_datang / 60);
							$secs = $luang_datang % 60;
							$dtime = (int)  sprintf ("%d:%2.1f",  $mins, $secs);
							$sfd =  '('. $dtime .')'; 
						}
						$timedeposit -= $dtime;
					} 
 
					//HITUNG WAKTU PULANG
					list($hp,$mp,$sp) = explode(":" , $names['faktanya_pulang'] );
					$faktanya_pulang 	= mktime( $hp, $mp, $sp, (int)$mm, (int)$dd, $yyyy); 
					list($hn,$mn,$sn) 	= explode(":" , $names['seharusnya_pulang'] );
					$seharusnya_pulang 	=  mktime( $hn, $mn, $sn, (int)$mm, (int)$dd, $yyyy); 
						
						if($faktanya_pulang >= $seharusnya_pulang){
							$luang_pulang 		= $faktanya_pulang - $seharusnya_pulang; 
							if ($luang_pulang >= 0) {
								$mins = floor ($luang_pulang / 60);
								$secs = $luang_pulang % 60;
								$sfp =  (int) sprintf ("%d:%2.1f",  $mins, $secs);
								
							}
							$timedepositgo += $sfp;
						}else{
							$luang_pulang 		= $seharusnya_pulang - $faktanya_pulang  ; 
							if ($luang_pulang > 0) {
								$mins = floor ($luang_pulang / 60);
								$secs = $luang_pulang % 60;
								$dtimep = (int)  sprintf ("%d:%2.1f",  $mins, $secs);
								$sfp =  '('. $dtimep .')'; 
							}
							$timedepositgo -= $dtimep;
						} 
					
			} 
		 
			$rows[] = array(  
				'tanggal'=>   date( 'd - m - Y' , strtotime($date.' 00:00:00') )  ,
				'jd'		=>  view_waktu_format($names['seharusnya_datang'])   ,
				'jp'		=>   view_waktu_format($names['seharusnyao_pulang'])   ,
				'wd'		=>   view_waktu_format($names['faktanya_datang'])  ,
				'wp'		=>   view_waktu_format($names['faktanya_pulang'])   ,
				'ti'		=> 	   $sfd ,
				'sd'		=>    $sfp    ,
				'sp'		=>  ( (trim($names['kehadiran']) =='') ? '-' : strtoupper($names['kehadiran']) ) ,
				'total'		=>  $names['keterangan']    , 
			); 
			 
		}else{
			$rows[] = array(  
				'tanggal'=>   date( 'd - m - Y' , strtotime($date.' 00:00:00') )  ,
				'jd'		=> ' - ' ,
				'jp'		=> ' - ' ,
				'wd'		=> ' - ' ,
				'wp'		=> ' - ' ,
				'ti'		=> ' - ' ,
				'sd'		=> ' - ' ,
				'sp'		=> ' - ' ,
				'total'		=> '   '  , 
			); 
		
		}
	}
		$rows[] = array(  
			'tanggal'=> '<b>S a l d o</b>',
			'jd'		=>  '  '  ,
			'jp'		=>  '  ' ,
			'wd'		=>  '  ' ,
			'wp'		=>  '  '  ,
			'ti'		=> $timedeposit ,
			'sd'		=> $timedepositgo ,
			'sp'		=>  '  '  ,
			'total'		=>  '  '   ,	   
		);   
		$datas = table_cetak_rows($rows); 
		$view .= table_cetak_builder($header , $datas ,  9 ,false );
	return $view;
}

 
?>
<style type="text/css">
<!--

	table.page_header {width: 100%; border: none; background-color: #CDCDCD; border-bottom: solid 1mm #000; padding: 2mm }
	table.page_footer {width: 100%; border: none;  border-top: solid 1mm #000; padding: 2mm}
div.zone
{
	border: solid 2mm #66AACC;
	border-radius: 3mm;
	padding: 1mm;
	background-color: #FFEEEE;
	color: #440000;
}
div.zone_over
{
	width: 30mm;
	height: 35mm;
	overflow: hidden;
}

-->
</style>
	
<page style="font-size: 12pt">
	<page_footer>
		<table class="page_footer">
			<tr>
				<td style="width: 33%; text-align: left;">
					Laporan absensi (<?php echo $m.'/'.$y?>)  <?php echo $karyawan['nama'];?> 
				</td>
				<td style="width: 34%; text-align: center">
					page [[page_cu]]/[[page_nb]]
				</td>
				<td style="width: 33%; text-align: right">
					HRIS RSKM * MAP&copy;2010 - <?php echo date('Y'); ?>
				</td>
			</tr>
		</table>
	</page_footer> 
		
		<div style="width: 100%; text-align:center; font-size: 14pt ;"><?php echo $karyawan['nama']; ?>   &nbsp; ( <?php echo  getage($karyawan['tanggal_lahir'])  ; ?>thn ) <br>
	
	<?php if(trim($karyawan['nik']) <> '' )echo '( NIP: '. strtoupper($karyawan['nik']) .' )';?></div>

	<br>
				<b><i>Tempat, tanggal lahir</i> :</b>	<?php echo ucfirst($karyawan['tempat_lahir']).', '. strtoupper($karyawan['tanggal_lahir']); ?><br> <br> 
				<b><i>Devisi / Jabatan</i> :</b>&nbsp;	<?php echo ucfirst($karyawan['devisi'] ) .' <b>/</b> '.$karyawan['jabatan']  ?><br><br>  
			 
	<br>   
	<?php echo list_data_absen(1, $m ,$y ,$_GET['id']); ?>
</page>  
<page style="font-size: 12pt">
	<page_footer>
		<table class="page_footer">
			<tr>
				<td style="width: 33%; text-align: left;">
					Laporan absensi (<?php echo $m.'/'.$y?>)  <?php echo $karyawan['nama'];?> 
				</td>
				<td style="width: 34%; text-align: center">
					page [[page_cu]]/[[page_nb]]
				</td>
				<td style="width: 33%; text-align: right">
					HRIS RSKM * MAP&copy;2010 - <?php echo date('Y'); ?>
				</td>
			</tr>
		</table>
	</page_footer>
	<?php echo list_data_absen( 2,$m ,$y ,$_GET['id']); ?>
</page>  